package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsTaskCategoryTypeEnum;
import qc.common.core.enums.EhsTaskDestObjectTypeEnum;
import qc.module.ehs.dto.taskcategory.TaskCategoryDto;
import qc.module.ehs.entity.EhsTaskCategory;

import java.util.List;

@Mapper
public interface EhsTaskCategoryMapper {
    EhsTaskCategoryMapper MAPPER = Mappers.getMapper(EhsTaskCategoryMapper.class);
    
    @Mapping(target = "objecttype",source = "objtype.index")
    @Mapping(target = "tasktype",source = "tasktype.index")
    TaskCategoryDto toDto(EhsTaskCategory en);
    
    List<TaskCategoryDto> toDtoList(List<EhsTaskCategory> ens);

    @Mapping(target = "objtype", ignore = true)
    @Mapping(target = "tasktype", ignore = true)
    EhsTaskCategory toEntity(TaskCategoryDto dto);

    /**
     * 新增/修改DTO转换为Entity，特殊处理
     * 
     * @param dto 新增/修改DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2024/9/25
     */
    default EhsTaskCategory dtoToEntity(TaskCategoryDto dto) {
        EhsTaskCategory en = toEntity(dto);
        
        en.setObjtype(getEhsTaskDestObjectTypeEnum(dto.getObjecttype()));
        en.setTasktype(getEhsTaskCategoryTypeEnum(dto.getTasktype()));
        
        return en;
    }

    /**
     * 根据枚举key获取枚举对象
     *
     * @param index
     * @return
     */
    static EhsTaskDestObjectTypeEnum getEhsTaskDestObjectTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsTaskDestObjectTypeEnum item : EhsTaskDestObjectTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为0--未定义
        return EhsTaskDestObjectTypeEnum.UNDEFINED;
    }

    /**
     * 根据枚举key获取枚举对象
     *
     * @param index
     * @return
     */
    static EhsTaskCategoryTypeEnum getEhsTaskCategoryTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsTaskCategoryTypeEnum item : EhsTaskCategoryTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //默认为null，任务大类必须有
        return null;
    }
    
}
